<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceEventInviteGuest extends core {

    public function process() {
        $limit = $this->limit;
        $page = intval($_POST['page']);
        $user_id = intval($_POST['user_id']);
        $event_id = intval($_POST['event_id']);
        $event = $this->db->get_row("SELECT * FROM events WHERE event_id=$event_id AND `user_id`='$user_id'");
        if ($event) {
            if ($page < 1) {
                $page = 1;
            }
            $from = ($page - 1) * $limit;
            $total = 0;

            $data = $this->db->get_results("SELECT * FROM guests WHERE user_id={$user_id} AND is_invite = 1 AND guest_id NOT IN (SELECT guest_id FROM guest_event WHERE event_id ={$event_id}) ORDER BY firstname asc LIMIT $from,$limit;");
            $total = $this->db->get_row("SELECT count(guest_id) as total FROM guests WHERE user_id={$user_id} AND is_invite = 1 AND guest_id NOT IN (SELECT guest_id FROM guest_event WHERE event_id ={$event_id});");

            if (ceil($total->total / $limit) <= $page) {
                $end_paging = 1;
            } else {
                $end_paging = 0;
            }

            $html = '';
            foreach ($data as $guest) {
                $html.='<div class="wd-add-guest-list">
                                <input type="checkbox" id="wd-checkbox-' . $guest->guest_id . '" rel="' . $guest->guest_id . '" name="fiance_condition" class="regular-checkbox-addguest checkbox-invite-guest">
                                <label class="label-cbx-invite" for="wd-checkbox-' . $guest->guest_id . '"></label>
                                <div class="wd-ava-addguest-list">';
                if ($guest->photo != "") {
                    $html.='<img src="'.$guest->photo.'" class="wd-addfbguest-ava"/>';
                } else {
                    $html.='<img src="img/no-ava.png" class="wd-addfbguest-ava"/>';
                }

                $html.='<span class="wd-addfbguest-name">' . $guest->firstname . ' ' . $guest->lastname . '</span>
                                </div>
                            </div>';
            }
            if ($html == '') {
                $html = '<div style="margin: 4px 12px 0 20px;color: #876d63;">No contact was found</div>';
            }
            $this->_JSONResponse(array('error' => 0, 'html' => $html, 'page' => $page + 1, 'end_paging' => $end_paging));
        }else{
             $this->_JSONResponse(array('error' => 1,'msg'=>"Event does not exist!"));
        }
    }

}

$processor = new webServiceEventInviteGuest();
$processor->process();
?>